import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * 动态配置行
 * @date 2019-07-15
 */
import { createPagination, getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { fetchHeaderList as _fetchHeaderList, fetchHeaderById as _fetchHeaderById, createHeader as _createHeader, updateHeader as _updateHeader, removeHeader as _removeHeader } from "../../services/dynamicForm/headerService";
export default {
  namespace: 'header',
  state: {
    list: [],
    configGroupList: [],
    pagination: {},
    headerDetail: {}
  },
  effects: {
    // 查询配置分类
    fetchConfigGroupList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConfigGroupList(_, _ref) {
      var call, put, res, configGroupList;
      return _regeneratorRuntime.wrap(function fetchConfigGroupList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUnifyIdpValue, 'CPFM.FORM_GROUP');

            case 3:
              res = _context.sent;
              configGroupList = getResponse(res);

              if (!configGroupList) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  configGroupList: configGroupList
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchConfigGroupList);
    }),
    // 查询列表
    fetchHeaderList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchHeaderList(_ref2, _ref3) {
      var payload, call, put, res, headerList;
      return _regeneratorRuntime.wrap(function fetchHeaderList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchHeaderList, payload);

            case 4:
              res = _context2.sent;
              headerList = getResponse(res);

              if (!headerList) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  pagination: createPagination(headerList),
                  list: headerList.content
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchHeaderList);
    }),
    // 通过 ID 查询
    fetchHeaderById: /*#__PURE__*/_regeneratorRuntime.mark(function fetchHeaderById(_ref4, _ref5) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchHeaderById$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_fetchHeaderById, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  headerDetail: list
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchHeaderById);
    }),
    // 创建
    createHeader: /*#__PURE__*/_regeneratorRuntime.mark(function createHeader(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createHeader$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_createHeader, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createHeader);
    }),
    // 更新
    updateHeader: /*#__PURE__*/_regeneratorRuntime.mark(function updateHeader(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateHeader$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_updateHeader, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateHeader);
    }),
    // 删除
    removeHeader: /*#__PURE__*/_regeneratorRuntime.mark(function removeHeader(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function removeHeader$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_removeHeader, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, removeHeader);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};